import uuid
from django.db import models
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField
from task.models import UserInfo


class Teacher(models.Model):
    """
    老师
    """
    name = models.CharField(verbose_name='姓名', max_length=16, unique=True)
    color = models.CharField(verbose_name='颜色', max_length=16, unique=True)

    def __str__(self):
        return self.name


class Subject(models.Model):
    """
    学科
    如：Linux、Python、Java
    """
    caption = models.CharField(verbose_name='学科名称', max_length=32)

    def __str__(self):
        return self.caption


class Course(models.Model):
    """
    课程
    如：全栈开发
    """
    caption = models.CharField(verbose_name='课程名称', max_length=32)
    subject = models.ForeignKey(verbose_name='学科', to='Subject')

    def __str__(self):
        return self.caption


class Module(models.Model):
    """
    计划模块模板
    """
    title = models.CharField(verbose_name='模块名称', max_length=32)
    course = models.ForeignKey(verbose_name='课程', to='Course')
    order = models.FloatField(verbose_name='排序', default=1)

    def __str__(self):
        return self.title


class Daily(models.Model):
    """
    每日计划教学模板
    """
    title = models.CharField(verbose_name='课程标题', max_length=64, db_index=True)
    detail = RichTextUploadingField(verbose_name='课程详细', config_name='basic', null=True, blank=True)
    module = models.ForeignKey(verbose_name='所属模块', to='Module')
    section_choices = (
        (1, '讲课'),
        (2, '考试'),
    )
    sec_type = models.IntegerField(verbose_name='类型', choices=section_choices, default=1)
    order = models.FloatField(verbose_name='序号', default=1)


class Class(models.Model):
    """
    班级
    """
    course = models.ForeignKey(verbose_name='课程', to='Course')
    num = models.IntegerField(verbose_name='班级(期)')
    start_date = models.DateField(verbose_name='开班时间')

    def __str__(self):
        return "%s%s期" % (self.course.caption, self.num,)


class TeachingDaily(models.Model):
    """
    实际教学日
    """
    cls = models.ForeignKey(verbose_name='班级', to='Class')
    module = models.ForeignKey(verbose_name='模块', to='Module')
    title = models.CharField(verbose_name='标题', max_length=64, db_index=True)
    section_choices = (
        (1, '讲课'),
        (2, '考试'),
    )
    sec_type = models.IntegerField(verbose_name='课时类型', choices=section_choices, default=1)
    order = models.FloatField(verbose_name='序号', default=1)
    teacher = models.ForeignKey(verbose_name='讲师', to='Teacher', related_name='teachers', null=True, blank=True)
    date = models.DateField(verbose_name='上课日期', null=True, blank=True)
    detail = RichTextUploadingField(verbose_name='课程详细', config_name='basic', null=True, blank=True)


class Tag(models.Model):
    """
    标签
    """
    title = models.CharField(verbose_name='标题', max_length=32)

    def __str__(self):
        return self.title


class QuestionLibrary(models.Model):
    """
    题库
    """
    title = models.CharField(verbose_name='标题', max_length=32)
    question = RichTextUploadingField(verbose_name='问题', config_name='question')
    answer = RichTextUploadingField(verbose_name='答案', config_name='question', null=True, blank=True)

    level_choices = (
        (1, '初级'),
        (2, '中级'),
        (3, '高级'),
    )
    level = models.SmallIntegerField(verbose_name='难度', choices=level_choices)
    tags = models.ManyToManyField(verbose_name='标签', to='Tag')
    score = models.PositiveIntegerField(verbose_name='分值')
    # 出题人
    creator = models.ForeignKey(verbose_name='出题人', to=UserInfo)


class Table(models.Model):
    table_type_choices = (
        (1, '排课'),
        (2, '任务管理'),
        (3, '学生管理'),
    )
    table_type = models.IntegerField(verbose_name='表格分类', choices=table_type_choices, default=1)
    name = models.CharField(verbose_name='sheet名称',max_length=16, db_index=True)
    text = models.TextField(verbose_name='表格内容配置')
    merge = models.TextField(verbose_name='表格合并单元格配置',null=True)
    style = models.TextField(verbose_name='表格样式配置',null=True)
    modifi = models.CharField(max_length=64, unique=True)

    def save(self, *args, **kwargs):
        self.modifi = str(uuid.uuid4())
        return super().save(*args, **kwargs)
